package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dto.ActivityListEditDTO;
import com.sc.nft.entity.PrizeDrawActivity;
import com.sc.nft.entity.dto.ActivityApproveDTO;
import com.sc.nft.entity.dto.ActivityListDTO;
import com.sc.nft.entity.vo.PrizeDrawActivityDetailsVO;
import com.sc.nft.entity.vo.PrizeDrawActivityVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.PrizeDrawActivityService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 抽奖活动(PrizeDrawActivity)表控制层
 *
 * @author wwc
 * @since 2023-08-22 14:07:44
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/prizeDrawActivity")
@Api(tags = "抽奖活动")
public class PrizeDrawActivityController {

    /**
     * 服务对象
     */
    private final PrizeDrawActivityService prizeDrawActivityService;

    @PostMapping(value = "/activityList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "活动列表", notes = "活动列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PrizeDrawActivityVO> activityList(@Validated @RequestBody ActivityListDTO activityListDTO) {
        return PageResult.success(prizeDrawActivityService.activityList(activityListDTO));
    }

    @PostMapping(value = "/approve", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "审批活动", notes = "审批活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result approve(@Validated @RequestBody ActivityApproveDTO activityApproveDTO) {
        if (activityApproveDTO.getAuditStatus().equals(2)) {
            if (StrUtil.isBlank(activityApproveDTO.getRemark())) {
                throw new GlobalRunTimeException("拒绝原因不能为空");
            }
        }
        prizeDrawActivityService.approve(activityApproveDTO);
        return Result.success();
    }


    @PostMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据id获取详情", notes = "根据id获取详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<PrizeDrawActivityDetailsVO> getById(@RequestBody IdRequest idRequest) {
        return Result.success(prizeDrawActivityService.getById(idRequest.getId()));
    }

    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑活动", notes = "编辑活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result edit(@Validated @RequestBody ActivityListEditDTO activityListEditDTO) {
        prizeDrawActivityService.edit(activityListEditDTO);
        return Result.success();
    }
}
